# coding=utf-8
"""
Author: Mr.夏末
@Function: web自动化：https://www.yuque.com/docs/share/73a7c983-4fcd-4bcf-8838-de0675b1738a
"""
import os,re,time,traceback

from selenium.webdriver import ActionChains
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

from class08.homework import Web
from selenium import webdriver

# 创建浏览器对象
# driver = webdriver.Chrome()
web = Web()
web.oprn_browser('gc')
driver = web.driver
# 添加隐式等待
driver.implicitly_wait(5)

# 打开网站
driver.get("http://47.105.110.138:8000/index.php/Home/user/login.html")
# 最大化浏览器
driver.maximize_window()

#ID定位
ele = driver.find_element('id','username')
# 操作元素:13800138006/2529085125@qq.com
ele.send_keys('2529085125@qq.com')

ele = driver.find_element('link text','淘宝网')
print(ele.get_attribute('outerHTML'))
# 部分文本
ele = driver.find_element("partial link text",'淘宝')
print(ele.get_attribute('outerHTML'))

# 输入密码
driver.find_element('xpath','//*[@id="password"]').send_keys('123456')
# 输入验证码
driver.find_element('xpath','//*[@id="verify_code"]').send_keys('1234')
# 点击登录
# driver.find_element('xpath','//*[@id="loginform"]/div/div[6]/a').click
ele = driver.find_element('partial link text','登')
# js获取返回值
s = driver.execute_script('return arguments[0].className',ele)
print(s)
# js点击登录
driver.execute_script('arguments[0].click()',ele)
# driver.execute_script('arguments[1].click();alert("arguments[0]")',111,ele)

# 获取昵称
nickname = driver.find_element('xpath','//a[@class="red userinfo"]').text
print(nickname)

#搜索
driver.find_element('xpath','//*[@id="q"]').send_keys('手机')
driver.find_element('xpath','//*[@id="sourch_form"]/a').click()

# 点击第1个加入购物车
driver.find_element('xpath','//a[contains(text(),"小米手机")]/../..//a[text()="加入购物车"]').click()
driver.find_element('id','join_cart').click()
# 强制等待
time.sleep(1)
driver.find_element('xpath','//*[@id="layui-layer1"]/span/a').click()

# 结算
driver.find_element('xpath','//*[@id="hd-my-cart"]/a/div/span').click()
driver.find_element('link text','去结算').click()
driver.find_element('class name','checkout-submit').click()

# 获取订单号
print(driver.find_element('xpath','//p[@class="succ-p"]').text)
orders = driver.find_element('xpath','//p[@class="succ-p"]').text
orderid = re.findall(r'\d{18}',orders)
print(orderid)
time.sleep(1)
# 点击我的订单
driver.find_element('link text','我的订单').click()

# 切换窗口
# 获取窗口的标志id列表
handles = driver.window_handles
print(handles)
# 下标切换
# driver.switch_to.window(handles[1])

# 标题切换
for i in handles:
    driver.switch_to.window(i)
    if driver.title.__contains__('我的订单'):
        break

# 搜索订单号
driver.find_element('xpath','//*[@id="search_key"]').send_keys(orderid)
driver.find_element('xpath','//*[@id="search_order"]/input[2]').click()

# 悬停
ele = driver.find_element('xpath','//span[text()="账户设置"]')
# 创建对象
action = ActionChains(driver)
# 鼠标移动到元素上
action.move_to_element(ele).perform()

# 修改个人信息
driver.find_element('xpath',"(//a[text()='个人信息'])[2]").click()
# driver.find_element('link text','个人信息').click()
# 图片上传
driver.find_element('xpath','//*[@id="preview"]').click()
# 进入iframe
iframe = driver.find_element('xpath','//iframe')
driver.switch_to.frame(iframe)
# 获取文件路径
path = os.path.join(os.path.dirname(os.path.dirname(__file__)),'lib','mm.jpg')
print(path)
# driver.find_element('xpath','//div[@id="filePicker"]/div[2]/input').send_keys(path)
# 如果有多个元素，默认取第一个元素
# ele = driver.find_element("xpath",'//input[@type="file"]')
# ele.send_keys(path)

# 如果有多个元素，怎么获取指定个数
# 方式1：(xpath)[2]    下标从1开始
# ele = driver.find_element("xpath",'(//input[@type="file"])[2]')
# ele.send_keys(path)

# 方式2：获取到所有元素，从元素列表里面取第几个
ele = driver.find_elements("xpath",'//input[@type="file"]')
ele[1].send_keys(path)
time.sleep(1)
# 显示等待
# WebDriverWait(driver,10,1).until(EC.text_to_be_present_in_element((By.XPATH,"//div[@class='info']"),'已上传1份'))

# 点击确定使用
driver.find_element('xpath','//div[@class="saveBtn"]').click()
driver.switch_to.default_content()
driver.find_element('xpath','//input[@class="save"]').click()

# 地址管理
driver.find_element('link text','地址管理').click()
# 删除地址
try:
    driver.find_element('xpath',"(//span[text()='哼哼哈嘿']/ancestor::ul//a[text()='删除'])[1]").click()
except:
    print(traceback.format_exc())
    print('没有地址可以删除')
finally:
    time.sleep(1)
# 新增地址
driver.find_element('xpath','//span[text()="增加新地址"]').click()
driver.find_element('xpath','//input[@name="consignee"]').send_keys('哼哼哈嘿')
driver.find_element('xpath','//input[@name="mobile"]').send_keys('18854725239')
# 找到select元素
ele = driver.find_element('xpath','//select[@id="province"]')
# 用select元素创建selenium的select对象
select = Select(ele)
# 下标选择
select.select_by_index(19)

ele = driver.find_element('xpath','//select[@id="city"]')
select = Select(ele)
# 值选择
select.select_by_value('28241')

# 文本选择
Select(driver.find_element('xpath','//select[@id="district"]')).select_by_visible_text('黄埔区')
Select(driver.find_element('xpath','//select[@id="twon"]')).select_by_visible_text('红山街道')

driver.find_element('xpath',"//input[@name='address']").send_keys("奔流到海不复回")
driver.find_element('xpath',"//input[@name='zipcode']").send_keys('272405')

driver.find_element('xpath','//a[@id="address_submit"]').click()